package sd.settemmezzo.player.state;

import java.io.Serializable;
import java.util.Vector;

public class Deck implements Serializable {
	private static final long serialVersionUID = 8437752660815108037L;
	
	private Vector<Carta> mazzo;
	
	public class Carta implements Serializable {
		private static final long serialVersionUID = -5669969826881583547L;
		
		private float punteggio;
		private String image;
		
		private Carta(float p, String i) {
			this.punteggio = p;
			this.image = i;
		}
		
		public float getPunteggio() {
			return this.punteggio;
		}
		
		public String getImage() {
			return this.image;
		}
	}
	
	/* Costruttore del mazzo base */
	public Deck() {
		mazzo = new Vector<Carta>();
		for (int i = 1; i <= 10; i++) {
			mazzo.add(new Carta(i > 7 ? 0.5f : i, "denari" + i + ".png"));
			mazzo.add(new Carta(i > 7 ? 0.5f : i, "bastoni" + i + ".png"));
			mazzo.add(new Carta(i > 7 ? 0.5f : i, "coppe" + i + ".png"));
			mazzo.add(new Carta(i > 7 ? 0.5f : i, "spade" + i + ".png"));
		}
		mescola();
 	}
	
	/* Costruttore del mazzo multiplo */
	public Deck(int size) {
		mazzo = new Vector<Carta>();
		for (int n = 0; n < size; n++) {
			for (int i=1; i <= 10; i++) {
				mazzo.add(new Carta(i > 7 ? 0.5f : i, "denari" + i + ".png"));
				mazzo.add(new Carta(i > 7 ? 0.5f : i, "bastoni" + i + ".png"));
				mazzo.add(new Carta(i > 7 ? 0.5f : i, "coppe" + i + ".png"));
				mazzo.add(new Carta(i > 7 ? 0.5f : i, "spade" + i + ".png"));
			}
		}
		mescola();
	}
	
	public void mescola() {
		int random1, random2;
		Carta temp;
		
		for (int i = 0; i < mazzo.size() * 10; i++) {
			random1 = (int) (Math.random() * mazzo.size());
			temp = mazzo.get(random1);
			random2 = (int) (Math.random() * mazzo.size());
			mazzo.set(random1, mazzo.get(random2));
			mazzo.set(random2, temp);
		}
	}
	
	public Carta estraiCarta() {
		Carta result;
		if (mazzo.size() > 0) {
			int random = (int) (Math.random() * mazzo.size());
			result = mazzo.get(random);
			mazzo.remove(random);
		} else {
			result = null;
		}
		return result;
	}
	
	public int size() {
		return mazzo.size();
	}

}
